<?php

namespace App\Models\Business;

use Illuminate\Database\Eloquent\Model;
use App\Models\Region as RegionModel;

class Address extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'business_address';

    protected $guarded = [];

    // 是否主动维护时间戳
    public $timestamps = false;

    protected $appends = [
        'region_text', //地区
    ];

    public function getRegionTextAttribute()
    {
        $region = '';

        $province = RegionModel::where('code', $this->attributes['province'])->value('name');

        if($province)
        {
            $region = $province;
        }

        $city = RegionModel::where('code', $this->attributes['city'])->value('name');

        if($city)
        {
            $region .= $city;
        }

        $district = RegionModel::where('code', $this->attributes['district'])->value('name');

        if($district)
        {
            $region .= $district;
        }

        return $region . $this->attributes['address'];
    }
}